import json

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import *


# Create your views here.
def index_view(request):
    # 模拟产品1的销售数据 - 实际项目中可以从数据库获取
    sales_data = {
        "regions": ["华东地区", "华南地区", "华北地区", "西部地区", "东北地区"],
        "sales_volume": [150, 240, 180, 120, 200],  # 销售量
        "sales_revenue": [45000, 72000, 54000, 36000, 60000]  # 销售额
    }

    # 表格数据
    table_data = [
        {"id": 1, "product": "产品A", "region": "华东地区", "sales_volume": 150, "sales_revenue": 45000,
         "date": "2024-01-15"},
        {"id": 2, "product": "产品A", "region": "华南地区", "sales_volume": 240, "sales_revenue": 72000,
         "date": "2024-01-16"},
        {"id": 3, "product": "产品A", "region": "华北地区", "sales_volume": 180, "sales_revenue": 54000,
         "date": "2024-01-17"},
        {"id": 4, "product": "产品B", "region": "华东地区", "sales_volume": 90, "sales_revenue": 27000,
         "date": "2024-01-15"},
        {"id": 5, "product": "产品B", "region": "华南地区", "sales_volume": 120, "sales_revenue": 36000,
         "date": "2024-01-16"},
        {"id": 6, "product": "产品C", "region": "华北地区", "sales_volume": 200, "sales_revenue": 60000,
         "date": "2024-01-17"},
        {"id": 7, "product": "产品C", "region": "西部地区", "sales_volume": 80, "sales_revenue": 24000,
         "date": "2024-01-18"},
        {"id": 8, "product": "产品A", "region": "东北地区", "sales_volume": 200, "sales_revenue": 60000,
         "date": "2024-01-19"},
        {"id": 9, "product": "产品B", "region": "西部地区", "sales_volume": 60, "sales_revenue": 18000,
         "date": "2024-01-18"},
        {"id": 10, "product": "产品C", "region": "东北地区", "sales_volume": 150, "sales_revenue": 45000,
         "date": "2024-01-19"},
    ]

    context = {
    #     'sales_data': json.dumps(sales_data),
    #     'table_data': json.dumps(table_data),
        'sales_data': sales_data,
        'table_data': table_data,
        'page_title': '产品销售分析仪表板'
    }

    return render(request, 'computers_home.html', context)


def test_view(request):
    sales_data = {
        "regions": ["华东地区", "华南地区", "华北地区", "西部地区", "东北地区"],
        "sales_volume": [150, 240, 180, 120, 200],  # 销售量
        "sales_revenue": [45000, 72000, 54000, 36000, 60000]  # 销售额
    }

    # 表格数据
    table_data = [
        {"id": 1, "product": "产品A", "region": "华东地区", "sales_volume": 150, "sales_revenue": 45000,
         "date": "2024-01-15"},
        {"id": 2, "product": "产品A", "region": "华南地区", "sales_volume": 240, "sales_revenue": 72000,
         "date": "2024-01-16"},
        {"id": 3, "product": "产品A", "region": "华北地区", "sales_volume": 180, "sales_revenue": 54000,
         "date": "2024-01-17"},
        {"id": 4, "product": "产品B", "region": "华东地区", "sales_volume": 90, "sales_revenue": 27000,
         "date": "2024-01-15"},
        {"id": 5, "product": "产品B", "region": "华南地区", "sales_volume": 120, "sales_revenue": 36000,
         "date": "2024-01-16"},
        {"id": 6, "product": "产品C", "region": "华北地区", "sales_volume": 200, "sales_revenue": 60000,
         "date": "2024-01-17"},
        {"id": 7, "product": "产品C", "region": "西部地区", "sales_volume": 80, "sales_revenue": 24000,
         "date": "2024-01-18"},
        {"id": 8, "product": "产品A", "region": "东北地区", "sales_volume": 200, "sales_revenue": 60000,
         "date": "2024-01-19"},
        {"id": 9, "product": "产品B", "region": "西部地区", "sales_volume": 60, "sales_revenue": 18000,
         "date": "2024-01-18"},
        {"id": 10, "product": "产品C", "region": "东北地区", "sales_volume": 150, "sales_revenue": 45000,
         "date": "2024-01-19"},
    ]

    context = {
        #     'sales_data': json.dumps(sales_data),
        #     'table_data': json.dumps(table_data),
        'sales_data': sales_data,
        'table_data': table_data,
        'page_title': '产品销售分析仪表板'
    }

    return render(request, 'computers_test.html', context)


# def full_data_view(request):
#     """用于展示全部数据的页面"""
#     # 这里可以返回更多的数据，为了示例我们使用相同的数据
#     table_data = [
#         # ... 更多的数据，可以是上面数据的扩展
#     ]
#
#     context = {
#         'table_data': json.dumps(table_data),
#         'page_title': '完整数据展示'
#     }
#     return render(request, 'analytics/full_data.html', context)